package com.jims.api.v1.controller.serve;

import com.baomidou.kisso.my.controller.BaseController;
import com.jims.master.common.data.Message;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.Encodes;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.modules.base.order.entity.Sorders;
import com.jims.master.modules.base.order.service.SordersService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.doctor.medicalService.entity.SDoctorPlan;
import com.jims.master.modules.doctor.medicalService.entity.SDoctorServe;
import com.jims.master.modules.doctor.medicalService.service.SDoctorPlanService;
import com.jims.master.modules.doctor.medicalService.service.SDoctorServeService;
import com.jims.master.modules.patient.myFriend.entity.SysUserMessage;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.UserService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 服务订单
 * @author WangYq
 * @version 2017-12-08
 */
@RestController
@RequestMapping(value = "/serve")
public class SordersController extends BaseController {
    @Autowired
    private SordersService sordersService;

    @Autowired
    private SDoctorServeService sDoctorServeService;

    @Autowired
    private SDoctorPlanService sDoctorPlanService;

    @Autowired
    private UserService userService;

    @Autowired
    private MasterDoctorinfoService masterDoctorinfoService;
    /**
     * 平台总累计服务人数
     * @author WangYQ
     * @return
     */
    @RequestMapping(value = "V100/personOrderCount",method ={RequestMethod.GET,RequestMethod.POST})
    public String personOrderCount() {
        Message msg=new Message();
        String count =sordersService.personOrderCount();
        msg.setData(count);
        return SecurityUtil.encryptMsgData(msg);
    }

    /**
     * 我的服务
     * @author WangYQ
     * @return
     */
    @RequestMapping(value = "V100/myService",method ={RequestMethod.GET,RequestMethod.POST})
    public String myService(@RequestParam(required = true)String baseinfoId) {
        Message msg=new Message();
        List<Sorders> sordersList =sordersService.myService(baseinfoId);
        msg.setData(sordersList);
        return SecurityUtil.encryptMsgData(msg);
    }

    /**
     * 订单列表(患者端)
     * @param-sorders.personId,sorders.doctorId,sorders.defaultId,sorders.planId,sorders.orderNo,sorders.orderState,sorders.title,sorders.startTime
     * @return
     * @author xueyx
     */
    @RequestMapping(value = "V100/sorders/list",method ={RequestMethod.GET,RequestMethod.POST})
    public String list(Sorders sorders )throws Exception
    {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Sorders> sordersList = sordersService.findList(sorders);
        List<Sorders> sordersList1 = new ArrayList<Sorders>();
        for(int i=0;i<sordersList.size();i++){
            Sorders s = sordersList.get(i);
            s.setOrderContent(Encodes.unescapeHtml(s.getOrderContent()));
            if (s.getOrderState().equals("0")) {
                Date now = new Date();
                long between = now.getTime()-s.getOrderDate().getTime();
                if(between/60000<30){
                    sordersList1.add(sordersList.get(i));
                }
            }else{
                sordersList1.add(sordersList.get(i));
            }
        }

        map.put("sordersList",sordersList1);
        Message mes = new Message();
        mes.setData(sordersList1);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 订单列表(医生端)
     * @param-sorders.personId,sorders.doctorId,sorders.defaultId,sorders.planId,sorders.orderNo,sorders.orderState,sorders.title,sorders.startTime
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "V100/sorders/patientList",method ={RequestMethod.GET,RequestMethod.POST})
    public String patientList(Sorders sorders )throws Exception
    {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Sorders> sordersList = sordersService.patientList(sorders);
        for(int i=0;i<sordersList.size();i++){
            Sorders s = sordersList.get(i);
            s.setOrderContent(Encodes.unescapeHtml(s.getOrderContent()));
        }
        map.put("sordersList",sordersList);
        Message mes = new Message();
        mes.setData(sordersList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 保存訂單
     * @param
     * @param-model
     * @author xueyx
     */
    @RequestMapping(value = "V100/sorders/save",method ={RequestMethod.GET,RequestMethod.POST})
    public String save(String id,String orderState) {
        Sorders sorders1 =sordersService.get(id);
        sorders1.setDoctorId(sorders1.getMasterDoctorinfo().getId());
        sorders1.setOrderState(orderState);
        int i = sordersService.save(sorders1);
        Message mes = new Message();
        if (i > 0) {
            mes.setData("保存订单信息成功");
        } else {
            mes.setData("保存订单信息失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 订单详情
     * @param sorders
     * @return
     * @throws Exception
     * @author xueyx
     */
    @RequestMapping(value = "V100/getOrders",method ={RequestMethod.GET,RequestMethod.POST})
    public String get(Sorders sorders )throws Exception
    {
        Map<String, Object> map = new HashMap<String, Object>();
        sorders = sordersService.get(sorders);
        sorders.setOrderContent(Encodes.unescapeHtml(sorders.getOrderContent()));
        String sinnum = sordersService.buyOrderCount(sorders.getMasterDoctorinfo().getId());
        //医生签约数
        sorders.getMasterDoctorinfo().setSignFamilCount(sinnum);
        Message mes = new Message();
        mes.setData(sorders);
        return SecurityUtil.encryptMsgData(mes);
        //return sorders;
    }
    /**
     * @Title: wxCheckCodeV100
     * @Description: （医生收入）
     * @param doctorId
     * @return java.lang.String
     * @author WangYQ
     * @date 2018-3-14
     * @throws
     */
    @RequestMapping(value = "V100/doctorIncome",method ={RequestMethod.GET,RequestMethod.POST})
    public String doctorIncome(@RequestParam(required = true)String doctorId ){
        Message msg = new Message();
        List<Sorders> sordersList=sordersService.doctorIncome(doctorId);
        msg.setData(sordersList);
        return SecurityUtil.encryptMsgData(msg);
    }

    /**
<<<<<<< HEAD
     * 医生上门服务下单
=======
     * 购买订单
>>>>>>> 4430d13cbf8853ee2b9324d23253e7aa3032df5d
     * @param planId
     * @param baseInfo
     * @param persionAddress
     * @param remarks
     * @param describ
<<<<<<< HEAD
=======
     * @author xueyx
>>>>>>> 4430d13cbf8853ee2b9324d23253e7aa3032df5d
     * @return
     */
    @RequestMapping(value = "V100/buy",method ={RequestMethod.GET,RequestMethod.POST})
    public String buy(String planId,String baseInfo,String persionAddress,String remarks,String describ) {
        Map<String, Object> map = new HashMap<String, Object>();
        Message mes = new Message();
        String result ="";
        String orderId ="";
        SDoctorPlan sDoctorPlan = new SDoctorPlan();
        sDoctorPlan.setId(planId);
        sDoctorPlan = sDoctorPlanService.get(sDoctorPlan);
        Integer i=sDoctorPlanService.findOrderedNumOfPlan(sDoctorPlan);
        if(i>=Integer.valueOf(sDoctorPlan.getPersonNum())){
            result = "购买失败，服务已经销售完毕";
        }else{
            SDoctorServe sDoctorServe =sDoctorServeService.get(sDoctorPlan.getServiceId());
            Sorders sordersSave = new Sorders();
            sordersSave.setPersonId(baseInfo);
            sordersSave.setDoctorId(sDoctorPlan.getDoctorId());
            sordersSave.setServiceId(sDoctorPlan.getServiceId());
            sordersSave.setDefaultId(sDoctorServe.getDefaultId());
            sordersSave.setOrderNo(sordersService.getOrderNo());
            sordersSave.setOrderDate(new Date());
            sordersSave.setOrderState("0");
            sordersSave.setTitle(sDoctorServe.getTitle());
            sordersSave.setPrice(sDoctorServe.getPrice());
            sordersSave.setDiscount(sDoctorServe.getDiscount());
            sordersSave.setCost(sDoctorServe.getCost());
            sordersSave.setOrderContent(sDoctorServe.getServeContent());
            sordersSave.setPicUrl(sDoctorServe.getPicUrl());
            if (describ==null){
                sordersSave.setDepict("无");
            }else{
                sordersSave.setDepict(describ);
            }
            sordersSave.setServeType(sDoctorServe.getServeType());
            sordersSave.setTimeType(sDoctorPlan.getTimeType());
            sordersSave.setStartTime(sDoctorPlan.getServiceDate());
            sordersSave.setPersionAddress(persionAddress);
            if(remarks==null){
                sordersSave.setRemarks("无");
            }else{
                sordersSave.setRemarks(remarks);

            }
            MasterDoctorinfo user = masterDoctorinfoService.get(sDoctorPlan.getDoctorId());
            MasterBaseinfo bserInfo = new MasterBaseinfo();
            bserInfo.setId(user.getBaseinfo().getId());
            MasterDoctorinfo masterDoctorinfo = masterDoctorinfoService.getDocByBaseinfo(bserInfo);
            if(masterDoctorinfo!=null)
                sordersSave.setHospital(masterDoctorinfo.getMechanism());
            int j=sordersService.save(sordersSave);
            if(j>0){
                result="购买成功";
                orderId = sordersSave.getId();
            }else{
                result="购买失败";
            }
        }
        map.put("result",result);
        map.put("orderId",orderId);
        mes.setData(map);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 家庭医生服务下单
     * @param baseInfo
     * @param remarks
     * @param describ
     * @return
     */
    @RequestMapping(value = "V100/buyFamily", method = {RequestMethod.GET, RequestMethod.POST})
    public String buyFamily(String serviceId, String baseInfo, String remarks, String describ) {
        Map<String, Object> map = new HashMap<String, Object>();
        Message mes = new Message();
        String result = "";
        String orderId = "";
        SDoctorServe sDoctorServe = sDoctorServeService.get(serviceId);
        Sorders sordersSave = new Sorders();
        sordersSave.setPersonId(baseInfo);
        sordersSave.setDoctorId(sDoctorServe.getDoctorId());
        sordersSave.setServiceId(sDoctorServe.getId());
        sordersSave.setDefaultId(sDoctorServe.getDefaultId());
        sordersSave.setOrderNo(sordersService.getOrderNo());
        sordersSave.setOrderDate(new Date());
        sordersSave.setStartTime(new Date());
        sordersSave.setOrderState("0");
        sordersSave.setTitle(sDoctorServe.getTitle());
        sordersSave.setPrice(sDoctorServe.getPrice());
        sordersSave.setDiscount(sDoctorServe.getDiscount());
        sordersSave.setCost(sDoctorServe.getCost());
        sordersSave.setOrderContent(sDoctorServe.getServeContent());
        sordersSave.setPicUrl(sDoctorServe.getPicUrl());
        if (describ == null) {
            sordersSave.setDepict("无");
        } else {
            sordersSave.setDepict(describ);
        }
        sordersSave.setServeType(sDoctorServe.getServeType());
        if (remarks == null) {
            sordersSave.setRemarks("无");
        } else {
            sordersSave.setRemarks(remarks);

        }
        MasterDoctorinfo user = masterDoctorinfoService.get(sDoctorServe.getDoctorId());
        MasterBaseinfo bserInfo = new MasterBaseinfo();
        bserInfo.setId(user.getBaseinfo().getId());
        MasterDoctorinfo masterDoctorinfo = masterDoctorinfoService.getDocByBaseinfo(bserInfo);
        if (masterDoctorinfo != null)
            sordersSave.setHospital(masterDoctorinfo.getMechanism());
        int j = sordersService.save(sordersSave);
        if (j > 0) {
            result = "购买成功";
            orderId = sordersSave.getId();
        } else {
            result = "购买失败";
        }
        map.put("result", result);
        map.put("orderId", orderId);
        mes.setData(map);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 患者端我的家庭医生服务列表
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "V100/getPatientServer",method ={RequestMethod.GET,RequestMethod.POST})
    public String getPatientServer(String  userId, @RequestParam(required = true)String pageNo, String pageSize, HttpServletRequest request, HttpServletResponse response )throws Exception
    {
        Page<Sorders> page = new Page<Sorders>();
        Map<String ,String> map = new HashMap<String, String>();
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        map.put("userId",userId);
        page = sordersService.getPatientServer(new Page<Sorders>(request, response), map);
        List<Sorders> sorders = page.getList();
        List<SDoctorServe> sdoc = new ArrayList<SDoctorServe>();
        Message mes = new Message();
        for(int i=0;i<sorders.size();i++){
            SDoctorServe sDoctorServe= sDoctorServeService.getServerByIds(sorders.get(i).getServiceId());
            if (sDoctorServe!=null) {
                sDoctorServe.setCreateDate(sorders.get(i).getCreateDate());
                sdoc.add(sDoctorServe);
            }
        }
        mes.setData(sdoc);
        return SecurityUtil.encryptMsgData(mes);
    }
}
